<?php
namespace app\finance\controller;
use think\Request;
use app\common\controller\Common;
class Finance extends Common
{
	/**
	* 财务列表
	*/
	public function index()
	{
		if(Request()->isGet()){
			$view = $this->indexTemp();
			/*@门店信息*/
			$shop = model('commin/Shop')->select();
			$this->assign('shop',$shop);
			return $this->fetch($view);
		}
	}
	/**
	* 总部-财务管理-估算
	*/
	public function estimate()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');
			$where = $this->handleIncome($data);
			/*@收入估算*/
			$income = model('Finance')->income($where);
			/*@支出估算*/
			$pay = model('Finance')->pay($where);
			$this->success(['income'=>$income,'pay'=>$pay]);
		}
	}

	/**************************** 私有 ****************************/
	/**
	* 财务列表模版
	*/
	private function indexTemp()
	{
		switch(session('admin.admin_type')){
			case 0: return 'all_index';break;//总部
			case 1:
			case 2:
			return 'yg_index';break;//店长+营业员
			default: $this->error(config('status.ERROR_SIX'));break;
		}
	}
	/**
	* 估算条件处理
	*/
	private function handleIncome($data)
	{
		$where = [];
		/*@门店*/
		if(isset($data['shop_id']) && $data['shop_id'] != ''){
			$where['shop_id'] = $data['shop_id'];
		}
		/*@时间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$where['addtime'] = [
				['egt', strtotime($data['start'])],
				['lt', strtotime($data['end'])]
			];
		}else if(isset($data['start']) && $data['start'] != ''){
			$where['addtime'] = ['egt', strtotime($data['start'])];
		}else if(isset($data['end']) && $data['end'] != ''){
			$where['addtime'] = ['lt', strtotime($data['end'])];
		}
		return $where;
	}
}